package com.example.emailontime.control;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


import android.util.Log;

public class ConnectEmail {
	private Session session;
	private Properties properties;
	private final String password;
	private final String username;

	public ConnectEmail(String username, String password) {
		this.username = username;
		this.password = password;
	}

	public boolean getSession() {
		boolean result = false;
		properties = getProperties();
		try {
			session = Session.getDefaultInstance(properties,
					new javax.mail.Authenticator() {
						protected PasswordAuthentication getPasswordAuthentication() {
							return new PasswordAuthentication(username,
									password);
						}
					});
			Log.i("Check", "Got session");
			result = true;
		} catch (Exception e) {
			System.out.println(e.toString() + "Session: Unknown Exception");
		}

		return result;
	}

	public Properties getProperties() {

		// connecting to the Gmail smtp
		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");
		Log.i("Check", "Got properties");
		return props;
	}

	public void sendEmail(String recipients, String subject, String content) {
		String finalString = "";

		boolean result = getSession();

		if (result) {
			// sending email
			try {
//				Log.i("Check", "Creating data handler");
//				DataHandler handler = new DataHandler(new ByteArrayDataSource(
//						finalString.getBytes(), "text/plain"));
//				Log.i("Check", "Data handler created");
				Message message = new MimeMessage(session);
				message.setFrom(new InternetAddress(username));
//				message.setDataHandler(handler);
		        Log.i("Check", "creating message");
		        //allows to "divide" the email in multiparts in case the email is too big
		        //Multipart multiPart=new MimeMultipart();
		        
				message.setRecipients(Message.RecipientType.TO,
						InternetAddress.parse(recipients));
				message.setSubject(subject);
				message.setText(content);

				 Log.i("check", "transport");
			        Transport transport = session.getTransport("smtp");
			        Log.i("check", "connecting");
			        transport.connect("smtp.gmail.com",username , password);
			        Log.i("check", "wana send");
			        transport.sendMessage(message, message.getAllRecipients());
			        transport.close();
			         
			        Log.i("check", "sent");


			
			} catch (MessagingException e) {
				throw new RuntimeException(e);
//			} catch (IOException e1) {
//
//				e1.printStackTrace();
//			}
		
			}} else
			System.out.println("Invalid session...");

	}
}
